package com.sbs.gaoxinqutoilet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.entity.AcceptanceEntity;
import com.sbs.gaoxinqutoilet.interfaces.StellarListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/6/14.
 * Time: 2018/6/14  15:36
 */

public class AccepterAdapter_beifen extends RecyclerView.Adapter<AccepterAdapter_beifen.AccepterViewHolder> {


    private Context context;
    private ArrayList<AcceptanceEntity> list;
    private LayoutInflater inflater;

    public AccepterAdapter_beifen(Context context, ArrayList<AcceptanceEntity>  list) {

        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public AccepterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_accepter, parent, false);

        return new AccepterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AccepterViewHolder holder, int position) {

        holder.name_tv.setText(list.get(position).getText());

        if (position%2==0){
            holder.image_a.setBackgroundResource(R.drawable.man);
        }else{
            holder.image_a.setBackgroundResource(R.drawable.woman);
        }




        holder.setData(position);

    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class AccepterViewHolder extends RecyclerView.ViewHolder{

        TextView name_tv;
        ImageView image_a,ima_selected;
        public AccepterViewHolder(View itemView) {
            super(itemView);

            name_tv= (TextView) itemView.findViewById(R.id.tv_accept);
            image_a=(ImageView) itemView.findViewById(R.id.image_accepter);
            ima_selected=(ImageView) itemView.findViewById(R.id.image_selected);



        }

        private List<String> acceepterlist=new ArrayList<>();
        private boolean isSelected;
        public void setData(final int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    if (stellistener!=null){
//                        stellistener.show(list.get(position).getText());
//                    }
                    if (!isSelected){
                        ima_selected.setBackground(ContextCompat.getDrawable(context,R.drawable.rb_yes));
                            acceepterlist.add(list.get(position).getText());
                    }else {
                        ima_selected.setBackground(ContextCompat.getDrawable(context,R.drawable.rb_no));
                        if (acceepterlist.contains(list.get(position).getText())){
                            acceepterlist.remove(list.get(position).getText());
                        }
                    }
                    isSelected=!isSelected;

                }
            });

        }
    }

    public void setStellarListener(StellarListener stellistener){
        this.stellistener=stellistener;
    }

    private StellarListener stellistener;
}
